package com.seeyon.apps.brwh.kit;

import com.alibaba.fastjson.JSONObject;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.time.temporal.TemporalAdjusters;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;

/**
 * 时间工具类
 */
public class DateUtil {

    /**
     * 获取当前时间前一天的时间
     * @param date
     * @return java.util.Date
     */
    public static Date beforeOneDayToNowDate(Date date,int day) {
        Calendar calendar = Calendar.getInstance();
        /* HOUR_OF_DAY 指示一天中的小时 */
        calendar.setTime(date);
        calendar.add(Calendar.DATE, day);
        return calendar.getTime();
    }


    /**
     * 获取当前时间前一小时的时间
     * @param date
     * @return java.util.Date
     */
    public static Date beforeOneHourToNowDate(Date date,int hour) {
        Calendar calendar = Calendar.getInstance();
        /* HOUR_OF_DAY 指示一天中的小时 */
        calendar.setTime(date);
        calendar.add(Calendar.HOUR_OF_DAY, hour);
        return calendar.getTime();
    }


    /**
     * 获取当前时间前一小时的时间   返回字符串
     * @param date
     * @return java.util.Date
     */
    public static String beforeOneHourToNowDate(Date date,int hour,String formt) {
        Calendar calendar = Calendar.getInstance();
        /* HOUR_OF_DAY 指示一天中的小时 */
        calendar.setTime(date);
        calendar.add(Calendar.HOUR_OF_DAY, hour);
        Date time = calendar.getTime();
        SimpleDateFormat format = new SimpleDateFormat(formt);
        String format1 = format.format(time);
        return format1;
    }




    /**
     * 获取当前时间前几分钟的时间   返回字符串
     * @param date
     * @return java.util.Date
     */
    public static String beforeMinuteToNowDate(Date date,int minute,String formt) {
        Calendar calendar = Calendar.getInstance();
        /* HOUR_OF_DAY 指示一天中的小时 */
        calendar.setTime(date);
        calendar.add(Calendar.MINUTE, minute);
        Date time = calendar.getTime();
        SimpleDateFormat format = new SimpleDateFormat(formt);
        String format1 = format.format(time);
        return format1;
    }


    /**
     * 获取当前时间前几分钟的时间   返回时间格式
     * @param date     当前时间
     * @param minute
     * @return
     */
    public static Date beforeMinuteToNowDate(Date date,int minute) {
        Calendar calendar = Calendar.getInstance();
        /* HOUR_OF_DAY 指示一天中的小时 */
        calendar.setTime(date);
        calendar.add(Calendar.MINUTE, minute);
        Date time = calendar.getTime();
        return time;
    }

    /**
     * 获取当前时间前几秒钟的时间   返回时间格式
     * @param date     当前时间
     * @param second
     * @return
     */
    public static Date beforeSecondToNowDate(Date date,int second) {
        Calendar calendar = Calendar.getInstance();
        /* HOUR_OF_DAY 指示一天中的小时 */
        calendar.setTime(date);
        calendar.add(Calendar.SECOND, second);
        Date time = calendar.getTime();
        return time;
    }


    /**
     * 获取当前时间后一小时的时间
     * @param date
     * @return java.util.Date
     */
    public static Date afterOneHourToNowDate(Date date) {
        Calendar calendar = Calendar.getInstance();
        /* HOUR_OF_DAY 指示一天中的小时 */
        calendar.setTime(date);
        calendar.add(Calendar.HOUR_OF_DAY, 1);
        return calendar.getTime();
    }


    /**
     * 获取时间差方法 返回 年，月，天，小时，分，秒
     * @param currentTime
     * @param firstTime
     * @return
     */
    public static HashMap<String,Object> dateDifferSecond(Date currentTime,Date firstTime){
        long diff = currentTime.getTime() - firstTime.getTime();//这样得到的差值是微秒级别
        Calendar  currentTimes =dataToCalendar(currentTime);//当前系统时间转Calendar类型
        Calendar  firstTimes =dataToCalendar(firstTime);//查询的数据时间转Calendar类型
        int year = currentTimes.get(Calendar.YEAR) - firstTimes.get(Calendar.YEAR);//获取年
        int month = currentTimes.get(Calendar.MONTH) - firstTimes.get(Calendar.MONTH);
        int day = currentTimes.get(Calendar.DAY_OF_MONTH) - firstTimes.get(Calendar.DAY_OF_MONTH);
        if (day < 0) {
            month -= 1;
            currentTimes.add(Calendar.MONTH, -1);
            day = day + currentTimes.getActualMaximum(Calendar.DAY_OF_MONTH);//获取日
        }
        if (month < 0) {
            month = (month + 12) % 12;//获取月
            year--;
        }
        long days = diff / (1000 * 60 * 60 * 24);
        long hours = (diff-days*(1000 * 60 * 60 * 24))/(1000* 60 * 60); //获取时
        long minutes = (diff-days*(1000 * 60 * 60 * 24)-hours*(1000* 60 * 60))/(1000* 60);  //获取分钟
        long s=(diff/1000-days*24*60*60-hours*60*60-minutes*60);//获取秒
        HashMap<String, Object> map = new HashMap<>();
        map.put("year",Math.abs(year));
        map.put("month",Math.abs(month));
        map.put("day",Math.abs(day));
        map.put("hours",Math.abs(hours));
        map.put("minutes",Math.abs(minutes));
        map.put("s",Math.abs(s));
        return map;
    }


    /**
     * 获取时间差方法 返回 年，月，天，小时，分，秒
     * @param currentTimeStr
     * @param firstTimeStr
     * @return
     */
    public static HashMap<String,Object> dateDifferSecondStr(String currentTimeStr,String firstTimeStr) throws ParseException {
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date currentTime = df.parse(currentTimeStr);
        Date firstTime = df.parse(firstTimeStr);
        long diff = currentTime.getTime() - firstTime.getTime();//这样得到的差值是微秒级别
        Calendar  currentTimes =dataToCalendar(currentTime);//当前系统时间转Calendar类型
        Calendar  firstTimes =dataToCalendar(firstTime);//查询的数据时间转Calendar类型
        int year = currentTimes.get(Calendar.YEAR) - firstTimes.get(Calendar.YEAR);//获取年
        int month = currentTimes.get(Calendar.MONTH) - firstTimes.get(Calendar.MONTH);
        int day = currentTimes.get(Calendar.DAY_OF_MONTH) - firstTimes.get(Calendar.DAY_OF_MONTH);
        if (day < 0) {
            month -= 1;
            currentTimes.add(Calendar.MONTH, -1);
            day = day + currentTimes.getActualMaximum(Calendar.DAY_OF_MONTH);//获取日
        }
        if (month < 0) {
            month = (month + 12) % 12;//获取月
            year--;
        }
        long days = diff / (1000 * 60 * 60 * 24);
        long hours = (diff-days*(1000 * 60 * 60 * 24))/(1000* 60 * 60); //获取时
        long minutes = (diff-days*(1000 * 60 * 60 * 24)-hours*(1000* 60 * 60))/(1000* 60);  //获取分钟
        long s=(diff/1000-days*24*60*60-hours*60*60-minutes*60);//获取秒
        HashMap<String, Object> map = new HashMap<>();
        map.put("year",StrKit.str(Math.abs(year)));
        map.put("month",StrKit.str(Math.abs(month)));
        map.put("day",StrKit.str(Math.abs(day)));
        map.put("hours",StrKit.str(Math.abs(hours)));
        map.put("minutes",StrKit.str(Math.abs(minutes)));
        map.put("s",StrKit.str(Math.abs(s)));
        return map;
    }

    /**
     * 传入分钟转化为多少小时多少分钟
     * @param liveTimeTotle
     * @return
     */
    public static String minutesToHoursAndMinutes(int liveTimeTotle){
        String hours = StrKit.str((int) Math.floor(liveTimeTotle / 60));
        String minute = StrKit.str(liveTimeTotle % 60);
        if(hours.length() == 1){
            hours = "0"+hours;
        }
        if(minute.length() == 1){
            minute = "0"+minute;
        }
        String decimal = hours +":"+minute;
//        Double decimal = Double.parseDouble(String.valueOf(hours).concat(".").concat(String.valueOf(minute)));
        return decimal;
    }

    /**
     * 传入分钟转化为多少小时多少分钟
     * @param liveTimeTotle
     * @return
     */
    public static String minutesToHour(int liveTimeTotle){
        String hours = StrKit.str((int) Math.floor(liveTimeTotle / 60));
        String minute = StrKit.str(liveTimeTotle % 60);
        if(hours.length() == 1){
            hours = "0"+hours;
        }
        if(minute.length() == 1){
            minute = "0"+minute;
        }
        String decimal = hours +"小时"+minute+"分钟";
        //        Double decimal = Double.parseDouble(String.valueOf(hours).concat(".").concat(String.valueOf(minute)));
        return decimal;
    }

    //Date类型转Calendar类型
    public static Calendar dataToCalendar(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        return calendar;
    }

    // 获取两个时间相差分钟数
    public static long getTime(String oldTime,String newTime) throws ParseException {
        long diff=0L;
        try {
            SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            long NTime =df.parse(newTime).getTime();
            //从对象中拿到时间
            long OTime = df.parse(oldTime).getTime();
             diff=(NTime-OTime)/1000/60;
        }catch (Exception e){
            SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
            long NTime =df.parse(newTime).getTime();
            //从对象中拿到时间
            long OTime = df.parse(oldTime).getTime();
            diff=(NTime-OTime)/1000/60;
        }
        return diff;
    }


    /**
     * 判断当前时间是否大于某个时间
     * @param Date  格式为 yyyy-MM-dd
     * @return
     * @throws ParseException
     */
    public static Boolean getDataGreaterThan(String Date) throws ParseException {
        SimpleDateFormat sp = new SimpleDateFormat("yyyy-MM-dd");
        Date parse = sp.parse(Date);
        long time1 = parse.getTime();
        //获取当前系统时间
        Date date = new Date();
        String format = sp.format(date);
        if(sp.parse(format).getTime()>time1){
            return true;
        }else{
            return false;
        }
    }


    /**
     * 获取指定时间的上个自然周开始时间
     * @param specifiedDate
     * @return
     */
    public static LocalDateTime getPreviousWeekStart(LocalDate specifiedDate) {
        // 获取指定日期所在周的周一
        LocalDate thisMonday = specifiedDate.with(TemporalAdjusters.previousOrSame(DayOfWeek.MONDAY));
        // 上一个自然周的开始时间就是这个周一减去一周
        return thisMonday.minusWeeks(1).atStartOfDay();
    }

    /**
     * 获取指定时间的上个自然周结束时间
     * @param specifiedDate
     * @return
     */
    public static LocalDateTime getPreviousWeekEnd(LocalDate specifiedDate) {
        // 获取指定日期所在周的周日
        LocalDate thisSunday = specifiedDate.with(TemporalAdjusters.nextOrSame(DayOfWeek.SUNDAY));
        // 上一个自然周的结束时间就是这个周日减去一周
        return thisSunday.minusWeeks(1).atTime(23, 59, 59);
    }

    /**
     * 将 LocalDateTime 对象格式化为 "yyyy-MM-dd HH:mm:ss" 格式
     *
     * @param dateTime 需要格式化的 LocalDateTime 对象
     * @return 格式化后的字符串
     */
    public static String formatDateTime(LocalDateTime dateTime) {
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        return formatter.format(dateTime);
    }


//    public static void main(String[] args) throws ParseException {
////        SimpleDateFormat sp = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//////        String date = "2023-04-14 14:00:00";
//////        String date2 = "2023-03-14 14:00:00";
//////        HashMap<String, Object> stringObjectHashMap = dateDifferSecond(sp.parse(date), sp.parse(date2));
//////        System.out.println(stringObjectHashMap.get("year")+"年"+stringObjectHashMap.get("month")+"月"+stringObjectHashMap.get("day")+"天"+stringObjectHashMap.get("hours")+"时"+stringObjectHashMap.get("minutes")+"分"+stringObjectHashMap.get("s")+"秒");
////        Date date = beforeOneDayToNowDate(new Date(), -2);
////        System.out.println(sp.format(date));
//
////        HashMap<String, Object> map = dateDifferSecondStr( "2024-07-07 16:00:00","2024-07-06 12:53:00");
////        String s = minutesToHoursAndMinutes(723);
////        long fk = 78;
////        int fkts = 9;
////        String s = minutesToHoursAndMinutes((int)Math.ceil((float) fk / fkts));
////        Boolean dataGreaterThanAndLessThan = getDataGreaterThan("2024-07-07");
//        long count = 5;
//        int l = (int)Math.ceil((float)count / 3);
//        int s = 78;
//        double v = Math.ceil(s / 60 / 24.0);
//        System.out.println(v);
//    }
}
